<!--
    * Time    : 2020-12-28 15:37:24
    * Author  : zhangTj
    * Desc    : 余额交易密码修改
-->

<template>
    <div class="w750 bg-fff">
        <div class="login">
            <div class="login-box">
                <div class="item">
                    <input type="text" class="item-input" v-model="phone" maxlength="11" readonly placeholder="请输入注册时使用的手机号" />
                </div>
                <div class="item code">
                    <input type="text" class="item-input" maxlength="6" v-model="code" placeholder="请输入短信验证码" />
                    <getvali-date codeType="updatePayPasswd" :codeMobile="phone" :class="'code-txt'" />
                </div>
                <div class="item">
                    <input type="password" class="item-input" v-model="pasw" placeholder="请输入新密码" />
                </div>
                <div class="item">
                    <input type="password" class="item-input" v-model="surePwd" placeholder="确认新密码" />
                </div>
                <div class="font-12 mt-1 text-999">密码由6-20位字母、数字或符号(除空格)组成</div>
            </div>
            <div class="b-btn" @click="register" :class="themes.theme"><span class="btn-r">确认修改</span></div>
        </div>
    </div>
</template>

<script>
import { mapState } from 'vuex'
import getvaliDate from 'components/getvaliDate/getvaliDate'
import { predepositUpdatePayPassword } from '@/api/balance.js'
import { getMyHome } from '@/api/my'

export default {
    components: {
        getvaliDate
    },
    data() {
        return {
            userName: '', // 手机号码，或者用户名，邮箱
            pasw: '', //密码
            surePwd: '', //再次输入密码
            phone: '', // 手机号码
            code: '', //验证码
            countDown: 0, // 倒计时
            agreement: false //注册协议
        }
    },
    computed: {
        ...mapState(['themes']),
        //检测手机号码是否正确
        phoneRight() {
            return /^1[0-9][0-9]{9}$/.test(this.phone)
        },
        //检测密码是否正确
        pswRegx() {
            return /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)]|[\(\)])+$)([^(0-9a-zA-Z)]|[\(\)]|[a-z]|[A-Z]|[0-9]){6,20}$/.test(this.pasw)
        },
        //检测密码是否正确
        pswRegx1() {
            return /^[0-9A-z`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘’，。、]+$/.test(this.pasw)
        }
    },
    mounted() {
        // 从session中获取用户信息，如果没有则调接口获取
        const userInfo = sessionStorage.getItem('userInfo')
        if (userInfo) {
            this.phone = JSON.parse(userInfo)
        } else {
            this.getUserInfo()
        }
    },
    methods: {
        // 获取用户信息
        getUserInfo() {
            getMyHome()
                .then((res) => {
                    this.phone = res.result.phone
                    this.$store.commit('SET_USERINFO', res.result)
                })
                .catch((err) => {
                })
        },

        //两次密码是否一致
        checkPwd() {
            if (this.pasw.length && this.surePwd.length && this.pasw == this.surePwd) {
            } else {
                this.$toast('两次密码不一致')
            }
        },
        //找回密码
        register() {
			if (!this.phone) {
			    this.$toast('请输入手机号')
			    return
			} else {
			    if (this.phoneRight == false) {
			        this.$toast('请输入正确的手机号码')
			        return
			    }
			}
			if (!this.code) {
			    this.$toast('请输入验证码')
			    return
			}
			if (!this.pasw) {
			    this.$toast('请输入新密码')
			    return
			}
			if (!this.surePwd) {
			    this.$toast('请再次输入新密码')
			    return
			}
			if (this.pasw && this.surePwd && this.pswRegx && this.pswRegx1) {
			    if (this.pasw.length && this.surePwd.length && this.pasw == this.surePwd) {
			    } else {
			        this.$toast('两次密码不一致')
			        return
			    }
			} else {
			    this.$toast('密码应为6-20位字母、数字和标点符号的组合')
			    return
			}
            this.$utils.uDebounce(() => {
				
				
				// 更新密码
				predepositUpdatePayPassword({
				    newPassword: this.surePwd,
				    smsCode: this.code
				})
				    .then((res) => {
				        if (!res.success) {
				            this.$toast(res.msg)
				            return
				        } else {
				            this.$toast('设置密码成功~')
							if(this.$route.query.subNumber) {
								this.$router.replace({ path: '/orderDetail', query: {subNumber:this.$route.query.subNumber}})
							} else if(this.$route.query.status) {
								this.$router.replace({ path: '/orderList', query: {status:this.$route.query.status}})
							} else {
								this.$router.go(-1)
							}
							
							
				        }
				    })
				    .catch((err) => {
				    })
			})
        }
    }
}
</script>

<style src="../../../assets/css/login.css" scoped></style>
